package com.isnotlocus.gulimail.ware.service.impl;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.isnotlocus.common.utils.R;
import com.isnotlocus.gulimail.product.entity.SkuInfoEntity;
import com.isnotlocus.gulimail.ware.Feign.SkuFeignService;
import com.isnotlocus.gulimail.ware.entity.PurchaseDetailEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isnotlocus.common.utils.PageUtils;
import com.isnotlocus.common.utils.Query;

import com.isnotlocus.gulimail.ware.dao.WareSkuDao;
import com.isnotlocus.gulimail.ware.entity.WareSkuEntity;
import com.isnotlocus.gulimail.ware.service.WareSkuService;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {
    @Autowired
    SkuFeignService skuFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<WareSkuEntity>();
        if (params.get("skuId") != null) {
            Long skuId = Long.valueOf(params.get("skuId").toString());
            wrapper.eq("sku_id", skuId);
        }
        if (params.get("wareId") != null) {
            Long wareId = Long.valueOf(params.get("wareId").toString());
            wrapper.eq("ware_id", wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void updateWare(PurchaseDetailEntity purchaseDetailEntity) throws IOException {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<WareSkuEntity>();
        Long skuId = purchaseDetailEntity.getSkuId();
        Long wareId = purchaseDetailEntity.getWareId();
        wrapper.eq("sku_id", skuId);
        wrapper.eq("ware_id", wareId);
        Integer count = this.baseMapper.selectCount(wrapper);
        if (count > 0) {
            this.baseMapper.UpdateWare(skuId, wareId, purchaseDetailEntity.getSkuNum());
        } else {
            R r = skuFeignService.getSkuName(skuId);
            System.out.println("r.get=>>>>>>>>>>>>>>>>>>" + r.get("skuInfo"));
            /**
             *     Map<String,Object> map= (Map<String, Object>) r.get("skuInfo");
             *      map.get("SkuName");
             *    可以代替下面这种 做法
             */

            WareSkuEntity skuEntity = new WareSkuEntity();
            //TODO 这是个多余字段，所以插不插入最好不设置事务回滚

            //1.让这个异常信息不影响事务的回滚
            //自己catch异常，如果出现异常则什么事都不做
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                SkuInfoEntity skuinfo = null;
                Object obj = r.get("skuInfo");
                skuinfo = objectMapper.convertValue(obj, new TypeReference<SkuInfoEntity>() {
                });
                skuEntity.setSkuName(skuinfo.getSkuName());
            } catch (Exception e) {

            }
            //TODO 还可以用什么方法让异常出现以后不需要回滚？高级部分讲解
            skuEntity.setSkuId(skuId);
            skuEntity.setWareId(wareId);
            skuEntity.setStock(purchaseDetailEntity.getSkuNum());

            skuEntity.setStockLocked(0);
            this.save(skuEntity);
        }

    }

}